(function($,win){
	$.ajaxSetup({cache: false});
	function ExamObj(examOption){	
		this.userExamId = examOption.userExamId;	
		this.basePath = examOption.basePath;
		this.allowSeeAnswer = examOption.allowSeeAnswer;
		this.allowSeeEorrer = examOption.allowSeeEorrer;
		this.confuseOrder = examOption.confuseOrder;
		this.confuseChoice = examOption.confuseChoice;
		this.isAddFavorite = examOption.isAddFavorite;
		this.examMainServer = examOption.favoriteUrl;	
	}
	ExamObj.prototype.getUserExamId=function(){
		return this.userExamId;
	}
	ExamObj.prototype.isEnterExam=function(){
		return false;
	}
	ExamObj.prototype.isConfuseOrder=function(){
		return this.confuseOrder;
	}
	ExamObj.prototype.isConfuseChoice=function(){
		return this.confuseChoice;
	}
	ExamObj.prototype.Init=function(client){
		
		var self=this;
		self.client=client;
		$(document).ajaxError(function(event, jqXHR, ajaxSettings, thrownError){/*对ajax发生错误的处理*/
			console.log(msg)
			 var msg='当前操作失败，请检查您的网络。点击<a href="'+window.location+'">这里</a>刷新。';
			 self.Message(msg, 'err');
		});
		var userExamIdUrl = self.basePath+'/myanswer/list/'+self.userExamId+'.json';
		console.log(userExamIdUrl)
		$.get(userExamIdUrl,{},function(data){	
			console.log(data)
			if(data&&data.success){
				self.client.__OnInited(data.answers); /*显示用户的答案*/
				var allowAddLink = self.allowSeeEorrer;
				$.get(self.basePath+'/answer/'+self.userExamId+'.json',{},function(data){			
					if(data&&data.success){
						var paperAnswers = data.answers;
						if(self.allowSeeAnswer){  /*显示正确答案*/
							self.client.__ShowAnswerAndHint(paperAnswers); /*显示正确答案和提示*/
						}
						self.client.__ShowUserAnswerRightStatus(paperAnswers,allowAddLink); 
						self.client.__InitLinks();
					}else{
						self.Message('获取试卷答案失败，请重试！', 'err');
					}		
				},'json');
				
			}else{
				self.Message('获取您的答案失败，请重试！', 'err');
			}			
		},'json');
	};	
	ExamObj.prototype.CloseDialog=function(){
		if(this.dialog){
			this.dialog.hide();
		}
	};
	ExamObj.prototype.Message=function(msg,flag){
		var self=this;
		if(!self.dialog){
			self.dialog=$('<div class="ui-popup-tip"></div>');
			$('#ui_wrapper').append(self.dialog);
			self.dialog.css('right','120px').css('top','120px');
		}
		var wrapper=$('<div class="ui-info ui-'+flag+'"></div>');
		self.dialog.empty().append(wrapper);
		if($.type(msg)=='string'){
			wrapper.append('<p>'+msg+'</p>');
		}else{
			wrapper.append($(msg));
		}
		var actionBar=$('<div class="ui-action-bar"></div>');
		if(arguments.length>2){
			var btns=arguments[2];
			if($.isArray(btns)){
				var len=btns.length,i;
				for(i=0;i<len;i++){
					var b=btns[i],btn=$('<button>'+b.label+'</button>');
					btn.on('click',b.func);actionBar.append(btn);
				}
			}
		}else{
			var c=$('<button>知道了</button>');
			actionBar.append(c);c.on('click',function(){self.CloseDialog();});
		}
		wrapper.append(actionBar);
		self.dialog.show();
	};
	ExamObj.prototype._errorQuestionsJSON = [];
	
	ExamObj.prototype._addToUserErrorQuestions=function(questId,json){
		var self = this;
		$.post("/exam/myQuestions/questions/add/" + questId + "/" + self.userExamId+"/json", {"json":json}, function (data) {
            if (!data.success) {
            	win.__ExamIns.show(data.msg, 'err');
                //self.Message(data.msg, 'err');
            }else{
            	win.__ExamIns.show(data.msg, 'pass');
            	//self.Message(data.msg, 'pass');
            }
        });
		//var self=this;
		//$.getScript(self.examMainServer+'/home/user/error/questions/add/'+questId+'/'+self.userExamId+'/jsonp/_addToUserErrorQuestionsCallback');
	};
	
	function _addToUserErrorQuestionsCallback(data){
		if(data&&data.success){
			var exist = data.exist;
			if(exist){
				win.__ExamIns.show('加入失败，当前题目已存在', 'err');
			}else{
				win.__ExamIns.show('加入成功，请在错题复习中查看', 'pass');
			}
		}else{
			win.__ExamIns.show(data.errMsg, 'err');
		}
	};	
	
	
	ExamObj.prototype.previewImage = function (myImage) {
		_previewImage(myImage);
	};
	
	ExamObj.prototype.showImage=function(msg){
		var self=this,width=750,height=468,win=$(window);
		if(!self.dialog){
			self.dialog=$('<div class="ui-popup-tip"></div>');
			self.dialog.attr('style','width:750px;height:468px;');
			$('#ui_wrapper').append(self.dialog);
		}
		self.dialog.css('left',(win.width()-width)/2).css('top',(win.height()-height)/2);
		var wrapper=$('<div style="padding:10px"></div>'),actionBar=$('<div class="ui-action-bar" style="text-align:right;padding-top:2px;"></div>'),closeBtn=$('<button>关闭</button>');
		self.dialog.empty().append(actionBar).append(wrapper);
		actionBar.append(closeBtn.on('click',function(){
			self.CloseDialog();
			self.dialog.attr('style','');
		}));
		wrapper.append($(msg));
		self.dialog.show();
	};
	ExamObj.prototype.show=function(msg,flag){
		var self=this;
		if(!self.dialog){
			self.dialog=$('<div class="ui-popup-tip"></div>');
			$('#ui_wrapper').append(self.dialog);
			self.dialog.css('left','550px').css('top','350px');
		}
		var wrapper=$('<div class="ui-info ui-info-'+flag+'"></div>');
		self.dialog.empty().append(wrapper);
		if($.type(msg)=='string'){
			wrapper.append('<p>'+msg+'</p>');
		}else{
			wrapper.append($(msg));
		}
		self.dialog.show();
		setTimeout(function(){self.CloseDialog();},1500);
	};
	win._addToUserErrorQuestionsCallback=_addToUserErrorQuestionsCallback;
	win.ExamObj=ExamObj;
})(jQuery,window);